<template>
  <div class="app-container">
    <!--查询-->
    <el-form :inline="true">
      <el-form-item label=" 公司名称">
        <el-input
          v-model="searchForm.companyName"
          placeholder=" 公司名称"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>
      <el-form-item label=" 省市区">
        <el-input
          v-model="searchForm.city"
          placeholder=" 省市区"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>

      <el-form-item label=" 办公地址">
        <el-input
          v-model="searchForm.address"
          placeholder=" 办公地址"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>
      <search-buttons
        :fetch-data="fetchList"
        :add-fun="addCompany"
        :export-flag="false"
      />
    </el-form>
    <!--表格-->
    <el-table :data="tableData" border stripe>
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="companyName" label=" 公司名称" />
      <el-table-column prop="regionsName" label=" 省市区" />
      <el-table-column prop="address" label=" 办公地址" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <operation-buttons
            :del-fun="delCompanyById"
            :row="scope.row"
            :edit-fun="editCompany"
            :detail-fun="companyDetail"
          />
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页 -->
      <pagination
        :page.sync="searchForm.currentPage"
        :total="total"
        :limit.sync="searchForm.pageSize"
        @pagination="fetchList"
      />
    </div>
  </div>
</template>
<script>
import company from '@/api/company'
import Pagination from '@/components/Pagination/index'
import SearchButtons from '@/components/SearchButtons/index'
import OperationButtons from '@/components/OperationButtons/index'
import { popDialog } from '@/utils/popDialog'
export default {
  components: {
    Pagination,
    SearchButtons,
    OperationButtons
  },
  data() {
    return {
      name: 'Company',
      searchForm: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      tableData: []
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      company.fetchList({ ...this.searchForm }).then(resp => {
        this.tableData = resp.data.data
        this.total = resp.data.total
        this.searchForm.pageNum = resp.data.pageNum
        this.searchForm.currentPage = resp.data.currentPage
      })
    },
    delCompanyById(id) {
      company.delById(id).then(resp => {
        this.$message.info(resp.message)
        this.fetchList()
      })
    },
    clearSearchForm() {
      this.searchForm = {}
    },
    downloadTemplate() {
      window.location.href = 'http://localhost:9527/company/template'
    },
    export2Excel() {
      company.export2Excel({ ...this.searchForm })
    },
    addCompany() {
      this.popForm(0, null)
    },
    editCompany(id) {
      this.popForm(1, id)
    },
    companyDetail(id) {
      this.popForm(2, id)
    },
    popForm(type, id) {
      popDialog('views/company/form', '400px', '300px', this.fetchList, { type, id })
    }
  }
}
</script>
<style>

</style>
